//
//  STTopView.swift
//  Demo-04-TabBarTest
//
//  Created by 苏统世 on 16/5/16.
//  Copyright © 2016年 SuTon. All rights reserved.
//

import UIKit

/// cell 布局的间隙距离
let cellMargin:CGFloat = 8

class STTopView: UIView {

    var data:STData?{
        didSet{

            
            if let urlString=data?.user?.profile_image_url{
                iconView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "avatar"))
                
            }
            
            
            nameLabel.text=data?.user?.screen_name
            menberIcon.image=data?.user?.verifiedImage
//            timeLabel.text=data?.created_at
            
            timeLabel.text=NSDate.date(data?.created_at ?? "").sinaDateDescription()
            
            sourceLabel.text=data?.source
            textLabel.text=data?.text
            middleView.pictureArray=data?.pictureArray
            
            let margin = data?.pictureArray.count>0 ? cellMargin:0
//            STPrint(items: "\(margin)")
            middleView.snp_updateConstraints { (make) -> Void in
                make.top.equalTo(textLabel.snp_bottom).offset(margin)
            }
            
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        setupUI()
    }

    
    
    private func setupUI(){
        self.addSubview(iconView)
        self.addSubview(nameLabel)
        self.addSubview(menberIcon)
        self.addSubview(timeLabel)
        self.addSubview(sourceLabel)
        self.addSubview(certifyIcon)
        self.addSubview(textLabel)
        self.addSubview(middleView)
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(cellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(cellMargin)
        }
        menberIcon.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(nameLabel.snp_trailing).offset(cellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel).offset(5)
            make.bottom.equalTo(iconView)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(cellMargin)
        }
        certifyIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(certifyIcon.snp_bottom).offset(2)
            
            make.leading.equalTo(iconView)
        }
        middleView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(2)
            make.leading.equalTo(textLabel)            
            make.size.equalTo(CGSize(width: 359, height: 359))
        }
        /// 设置原创view的底部和contentLabel的底部重合偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(middleView).offset(cellMargin)
        }
    }
    
    lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    lazy var nameLabel:UILabel = STLabel.addLabel(title: "昵称测试")
    
    lazy var menberIcon:UIImageView=UIImageView(image: UIImage(named: "common_icon_membership"))
    
    lazy var timeLabel:UILabel = STLabel.addLabel(title: "2016-5-13  21-47", color: UIColor.orangeColor(), titleFont: UIFont.systemFontOfSize(13))

    lazy var sourceLabel:UILabel = STLabel.addLabel(title: "www.baidu.com", color: UIColor.lightGrayColor(), titleFont: UIFont.systemFontOfSize(13))
    
    lazy var certifyIcon:UIImageView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
    
    lazy var textLabel:UILabel = {
        let txtlabel = STLabel.addLabel(title: "我是测试文本我是测试文本我是AB HJYZ测试文本我是测试文本我是测试文本我是测试文本")
        txtlabel.numberOfLines = 0
        txtlabel.font=UIFont.systemFontOfSize(14)
        // 最大换行宽度
        txtlabel.preferredMaxLayoutWidth = kScreen_W-2*cellMargin
        txtlabel.backgroundColor=UIColor.lightGrayColor()
        return txtlabel
    }()
    lazy private var middleView:STPictureView = STPictureView()
}
